package com.cloud.school.app.controller;

import com.cloud.school.app.config.security.SecurityUserUtils;
import com.cloud.school.app.model.GroupMemberModel;
import com.cloud.school.app.model.GroupModel;
import com.cloud.school.app.service.GroupMemberService;
import com.cloud.school.app.service.GroupService;
import com.cloud.school.app.service.impl.CacheService;
import com.cloud.school.app.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.cloud.school.app.service.impl.ChatWebSocketHandler.USER_ID_SESSION_ID_MAP_KEY;

/**
 * @ClassName Group
 * @Description TODO
 * @Author 36297
 * @Date 2018/12/14 1:05
 * @Version 1.0
 */
@Api(tags = "群组管理控制器")
@RestController
@RequestMapping("/group")
public class GroupController {

    @Autowired
    private GroupService groupService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private GroupMemberService groupMemberService;

    @ApiOperation("所有群组")
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ResponseUtil<List<GroupModel>> all(@ApiParam("专业id") @RequestParam Long majorId) {
        return ResponseUtil.ok(groupService.selectByMajorId(majorId));
    }

    @ApiOperation("创建群组")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseUtil<Boolean> create(@RequestBody GroupModel groupModel) {
        groupModel.setCreateUid(SecurityUserUtils.getCurrentUserId());
        return ResponseUtil.ok(groupService.save(groupModel));
    }

    @ApiOperation("群组添加用户")
    @RequestMapping(value = "/addMember", method = RequestMethod.POST)
    public ResponseUtil<Boolean> addMember(@RequestBody GroupMemberModel groupMemberModel) {
        return ResponseUtil.ok(groupMemberService.save(groupMemberModel));
    }

    @ApiOperation("删除群组")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseUtil<Boolean> delete(@ApiParam("群组id") @RequestParam Long groupId) {
        return ResponseUtil.ok(groupService.delete(groupId));
    }

    @ApiOperation("群组删除用户")
    @RequestMapping(value = "/deleteMember", method = RequestMethod.POST)
    public ResponseUtil<Boolean> deleteMember(@ApiParam("群组id") @RequestParam Long groupId, @ApiParam("用户id(可以多个，使用,分隔)") @RequestParam(value = "userId") List<Long> userIds) {
        return ResponseUtil.ok(groupMemberService.deleteByGroupIdAndUserIds(groupId, userIds));
    }

    @ApiOperation("邀请用户加入群组")
    @RequestMapping(value = "/inviteUser", method = RequestMethod.POST)
    public ResponseUtil<Boolean> inviteUser(@ApiParam("群组id") @RequestParam Long groupId, @ApiParam("用户id(可以多个，使用,分隔)") @RequestParam(value = "userId") List<Long> userIds) {
        return ResponseUtil.ok(groupMemberService.inviteUser(groupId, userIds, SecurityUserUtils.getCurrentUserId()));
    }

    @ApiOperation("用户同意加入群组")
    @RequestMapping(value = "/passInvitation", method = RequestMethod.POST)
    public ResponseUtil<Boolean> passInvitation(@ApiParam("群组成员记录id,不是用户id") @RequestParam(value = "groupMemberId") Long groupMemberId) {
        return ResponseUtil.ok(groupMemberService.passInvitation(groupMemberId));
    }

    @ApiOperation("查询群成员")
    @RequestMapping(value = "/queryMember", method = RequestMethod.GET)
    public ResponseUtil<List<GroupMemberModel>> queryMember(@ApiParam("群组id") @RequestParam Long groupId) {
        return ResponseUtil.ok(groupMemberService.queryDetailByGroupId(groupId));
    }

    @ApiOperation("查询群推荐")
    @RequestMapping(value = "/queryInvitation", method = RequestMethod.GET)
    public ResponseUtil<List<GroupMemberModel>> queryInvitation(@ApiParam("用户id") @RequestParam Long userId) {
        return ResponseUtil.ok(groupMemberService.queryInvitation(userId));
    }

    @ApiOperation("查询群在线成员")
    @RequestMapping(value = "/queryOnlineMember", method = RequestMethod.GET)
    public ResponseUtil<List<GroupMemberModel>> queryOnlineMember(@ApiParam("群组id") @RequestParam Long groupId) {
        List<GroupMemberModel> list = groupMemberService.queryDetailByGroupId(groupId);
        Set<String> userIds = cacheService.hashKeys(USER_ID_SESSION_ID_MAP_KEY);
        List<GroupMemberModel> result = list.stream().filter(f -> userIds.contains(f.getUserId().toString())).collect(Collectors.toList());
        return ResponseUtil.ok(result);
    }
}
